package com.example.mycrimelntent;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CalendarView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.mycrimelntent.databinding.DataPickDialogBinding;

import java.util.Date;

public class DatePickDialog extends DialogFragment {
    private DataPickDialogBinding binding;
    public DatePickDialog(){

    }

    public static DatePickDialog newInstance(String param1){
        DatePickDialog fragment = new DatePickDialog();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null){

        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataPickDialogBinding.inflate(inflater, container, false);
        //设置日期变更时间
        binding.calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
                //设置日期
                Date date = new Date(i - 1900, i1 - 1, i2);
                //使用bundle加载数据
                Bundle bundle=new Bundle();
                bundle.putString("日期",date.toString());
                //数据传输
                FragmentManager fragmentManager=getParentFragmentManager();
                fragmentManager.setFragmentResult("Date",bundle);
                DatePickDialog.this.dismiss();
            }
        });
        return binding.getRoot();
    }
}
